package com.jungle.app.jw.service.impl;

import com.jungle.app.domain.entity.JwRoute;
import com.jungle.app.jw.domain.bo.JwRouteBo;
import com.jungle.app.jw.domain.vo.JwRouteVo;
import com.jungle.app.jw.repository.JwRouteRepository;
import com.jungle.app.jw.service.IJwRouteService;
import com.jungle.common.core.domain.PageQuery;
import com.jungle.common.core.page.TableDataInfo;
import com.jungle.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 路线Service业务层处理
 *
 * @author jungle
 * @date 2023-12-04
 */
@RequiredArgsConstructor
@Service
public class JwRouteServiceImpl implements IJwRouteService {

    private final JwRouteRepository repository;

    /**
     * 查询路线
     */
    @Override
    public JwRouteVo queryById(String id){
        return repository.findById(id).map(v -> com.jungle.common.utils.BeanCopyUtils.copy(v, JwRouteVo.class)).orElse(null);
    }

    /**
     * 查询路线列表
     */
    @Override
    public TableDataInfo<JwRouteVo> queryPageList(JwRouteBo bo, PageQuery pageQuery) {
        Page<JwRoute> result = repository.findAll(getSpecification(bo), pageQuery.pageable());
        return TableDataInfo.build(result, JwRouteVo.class);
    }

    /**
     * 查询路线列表
     */
    @Override
    public List<JwRouteVo> queryList(JwRouteBo bo) {
        return repository.findAll(getSpecification(bo)).stream()
            .map(v -> com.jungle.common.utils.BeanCopyUtils.copy(v, JwRouteVo.class))
            .collect(Collectors.toList());
    }

    private Specification<JwRoute> getSpecification(JwRouteBo bo) {
        return (root, query, cb) -> {
            Map<String, Object> params = bo.getParams();
            List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(bo.getName())) {
                    predicates.add(cb.like(root.get("name"), "%" + bo.getName() + "%"));
            }
            if (StringUtils.isNotBlank(bo.getPrice())) {
                    predicates.add(cb.equal(root.get("price"), bo.getPrice()));
            }
            if (StringUtils.isNotBlank(bo.getSetDate())) {
                    predicates.add(cb.equal(root.get("setDate"), bo.getSetDate()));
            }
            if (StringUtils.isNotBlank(bo.getWay())) {
                    predicates.add(cb.equal(root.get("way"), bo.getWay()));
            }
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        };
    }

    /**
     * 新增路线
     */
    @Override
    public Boolean insertByBo(JwRouteBo bo) {
        JwRoute add = com.jungle.common.utils.BeanCopyUtils.copy(bo, JwRoute.class);
        validEntityBeforeSave(add);
        repository.saveAndFlush(add);
        bo.setId(add.getId());
        return true;
    }

    /**
     * 修改路线
     */
    @Override
    public Boolean updateByBo(JwRouteBo bo) {
        JwRoute update = com.jungle.common.utils.BeanCopyUtils.copy(bo, JwRoute.class);
        validEntityBeforeSave(update);
        return repository.save(update) != null;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JwRoute entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路线
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        repository.deleteAllById(ids);
        return true;
    }

}
